package com.maibow.controller.test;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maibow.controller.base.BaseController;
import com.maibow.util.AppUtil;
import com.maibow.util.CommonConstants;
import com.maibow.util.MapDistance;
import com.maibow.util.PageData;
import com.maibow.util.PathUtil;
import com.maibow.util.TwoDimensionCode;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.controller.test
 * 文件名：ToolController.java
 * 作者: 吴比(Wubi)
 * 时间：2015年5月3日-上午9:08:56
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Controller
@RequestMapping(value = "/tool")
public class ToolController extends BaseController {

    /**
     * 二维码页面
     */
    @RequestMapping(value = "/goTwoDimensionCode")
    public ModelAndView goTwoDimensionCode() throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        mv.setViewName("system/tools/twoDimensionCode");
        mv.addObject("pd", pd);
        return mv;
    }

    /**
     * 生成二维码
     * 
     * @param args
     * @throws Exception
     */
    @RequestMapping(value = "/createTwoDimensionCode")
    @ResponseBody
    public Object createTwoDimensionCode() {
        Map<String, String> map = new HashMap<String, String>();
        PageData pd = new PageData();
        pd = this.getPageData();
        String errInfo = "success", encoderImgId = this.get32UUID() + ".png"; // encoderImgId此处二维码的图片名
        String encoderContent = pd.getString("encoderContent"); // 内容
        if (null == encoderContent) {
            errInfo = "error";
        } else {
            try {
                String filePath = PathUtil.getClasspath() + CommonConstants.FILEPATHTWODIMENSIONCODE; // 存放路径
                TwoDimensionCode.encoderQRCode(encoderContent, filePath, encoderImgId, "png"); // 执行生成二维码
            } catch (Exception e) {
                errInfo = "error";
            }
        }
        map.put("result", errInfo); // 返回结果
        map.put("encoderImgId", encoderImgId); // 二维码图片名
        return AppUtil.returnObject(new PageData(), map);
    }

    /**
     * 解析二维码
     * 
     * @param args
     * @throws Exception
     */
    @RequestMapping(value = "/readTwoDimensionCode")
    @ResponseBody
    public Object readTwoDimensionCode() {
        Map<String, String> map = new HashMap<String, String>();
        PageData pd = new PageData();
        pd = this.getPageData();
        String errInfo = "success", readContent = "";
        String imgId = pd.getString("imgId");// 内容
        if (null == imgId) {
            errInfo = "error";
        } else {
            try {
                String filePath = PathUtil.getClasspath() + CommonConstants.FILEPATHTWODIMENSIONCODE + imgId; // 存放路径
                readContent = TwoDimensionCode.decoderQRCode(filePath);// 执行读取二维码
            } catch (Exception e) {
                errInfo = "error";
            }
        }
        map.put("result", errInfo); // 返回结果
        map.put("readContent", readContent); // 读取的内容
        return AppUtil.returnObject(new PageData(), map);
    }

    /**
     * 地图页面
     */
    @RequestMapping(value = "/map")
    public ModelAndView map() throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        mv.setViewName("system/tools/map");
        mv.addObject("pd", pd);
        return mv;
    }

    /**
     * 获取地图坐标页面
     */
    @RequestMapping(value = "/mapXY")
    public ModelAndView mapXY() throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        mv.setViewName("system/tools/mapXY");
        mv.addObject("pd", pd);
        return mv;
    }

    /**
     * 根据经纬度计算距离
     * 
     * @param args
     * @throws Exception
     */
    @RequestMapping(value = "/getDistance")
    @ResponseBody
    public Object getDistance() {
        Map<String, String> map = new HashMap<String, String>();
        PageData pd = new PageData();
        pd = this.getPageData();
        String errInfo = "success", distance = "";
        try {
            distance = MapDistance.getDistance(pd.getString("ZUOBIAO_Y"), pd.getString("ZUOBIAO_X"), pd.getString("ZUOBIAO_Y2"), pd.getString("ZUOBIAO_X2"));
        } catch (Exception e) {
            errInfo = "error";
        }
        map.put("result", errInfo); // 返回结果
        map.put("distance", distance); // 距离
        return AppUtil.returnObject(new PageData(), map);
    }

}
